
rem
bbdoc: Simple Newton physics system. It handles acceleration, velocity and friction.
about: Required components: TPosition, TPhysics.
end rem
Type TPhysicsSystem Extends TEntityProcessingSystem

	Field _physicsMapper:TComponentMapper
	Field _positionMapper:TComponentMapper
	
	
	Method Initialize()
		_physicsMapper = TComponentMapper.Create(New TPhysicsComponent, _world)
		_positionMapper = TComponentMapper.Create(New TPositionComponent, _world)
		RegisterComponent(New TPhysicsComponent)
		RegisterComponent(New TPositionComponent)
	End Method
	
	
	Method ProcessEntity(e:TEntity)

		'get components of this entity
		Local v:TPhysicsComponent = TPhysicsComponent(_physicsMapper.Get(e))
		Local p:TPositionComponent = TPositionComponent(_positionMapper.Get(e))
		
		'determine new velocity
		Local velocity:TVector2D = v.GetVelocity()
		velocity.AddV(v.GetAcceleration())
		'apply friction
		velocity.Multiply(1.0:Float - v.GetFriction())
		
		v.SetVelocityV(velocity)
		v.ResetAcceleration()
		
		Local vx:Float = velocity.GetX() * _world.GetDelta()
		Local vy:Float = velocity.GetY() * _world.GetDelta()
		'add velocity to position
		p.Add(vx, vy)
	End Method
		
End Type
